/* ACN IDC for AAS. 
   27 Jan 2011
   LockProduct: The product object will be locked, the User will not be able to add any products at 90% Opty
   LockQuote: Users will not be able to edit quote at 90%opty. Recordtype changed to RO pagelayout
   ValidateFulfillmentCompleted :Opty cannot be moved to 100% unless all orders are fulfilled*/
   
   public class AASOpportunityCheck{

    public static void LockProduct(Map<id,OpportunityLineItem> OppProdMap, boolean isInsert,boolean isDelete, boolean isUpdate){
        ID AASRecordTypeID = RecordTypeHelper.GetRecordTypeIDByDevName('Opportunity', 'Advanced_Ad_Solutions');
        //Map <String,Schema.RecordTypeInfo> recordTypes =  Opportunity.sObjectType.getDescribe().getRecordTypeInfosByName();
        //Id AASRecordTypeID = recordTypes.get('Advanced Ad Solutions').getRecordTypeId();
        List<OpportunityLineItem> OptyProducts = [Select id,Opportunity.recordtypeId ,Opportunity.StageName,Opportunity.Probability  from OpportunityLineItem where id in :OppProdMap.keySet()];
        for(OpportunityLineItem OL :OptyProducts){
            if((OL.Opportunity.Probability == 90.00 || OL.Opportunity.Probability ==100.00) && OL.Opportunity.recordtypeId ==AASRecordTypeID){
                OpportunityLineItem lineItem = OppProdMap.get(OL.Id);
                //if(isInsert!=null && isInsert)
                    //lineItem.addError('You cannot add products to "Signed" or "Fulfilled" Opportunity');
                  if(isDelete!=null && isDelete)
                    lineItem.addError('You cannot delete products from "Signed" or "Fulfilled" Opportunity');
                //else if(isUpdate!=null && isUpdate)
                    //lineItem.addError('You cannot edit products of "Signed" or "Fulfilled" Opportunity');

            }
        }
    }
    
 /*   public static void ValidateQuoteInsert(Map<id,Quote> QuoteMap){
        //ID AASRecordTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'Advanced Ad Solutions');
        Map <String,Schema.RecordTypeInfo> recordTypes =  Opportunity.sObjectType.getDescribe().getRecordTypeInfosByName();
        Id AASRecordTypeID = recordTypes.get('Advanced Ad Solutions').getRecordTypeId();
        List<Quote> Quot = [Select id,Opportunity.recordtypeId ,Opportunity.StageName,Opportunity.Probability  from Quote where id in :QuoteMap.keySet()];
        for(Quote Q :Quot){
            if((Q.Opportunity.Probability == 90.00 || Q.Opportunity.Probability ==100.00) && Q.Opportunity.recordtypeId ==AASRecordTypeID){
                Quote Qt = QuoteMap.get(Q.Id);
                Qt.addError('You cannot add Quotes to "Signed" or "Fulfilled" Opportunity');
            }
        }
    }*/
    
       public static void DeleteExistingOrders(Set<id> OptyIds){
        //ID AASQuoteRecordTypeID = RecordTypeHelper.GetRecordTypeID('Quote', 'Advanced Ad Solutions');
        //Map <String,Schema.RecordTypeInfo> recordTypes =  Quote.sObjectType.getDescribe().getRecordTypeInfosByName();
        //Id AASQuoteRecordTypeID = recordTypes.get('Advanced Ad Solutions').getRecordTypeId();        
        List<Quote> QuoteList = new List<Quote>();
        List<Opportunity> OptyList = new List<Opportunity>();
        List<Opportunity> Opty = [select order_initiated__c from opportunity where id in :OptyIds and order_initiated__c = true];
        List<Order__C> oldOrders = [Select id from order__c where opportunity__c in :OptyIds and opportunity__r.order_initiated__c =true];
        //List<Quote> Q = [select recordtypeid from quote where opportunityid in :OptyIds];
        try{if(oldOrders.size()>0) Delete oldOrders;}
        catch(dmlException e){
                 system.debug(e);
                 GeneralConstants.mailAnyExceptionToMe('AASOpportunityCheck', 'Delete oldOrders fail', e);
        }
        for(Opportunity op : Opty){
            op.order_initiated__c = false;
            optylist.add(op);            
        }
         if(OptyList.size() >0){
             try{Update OptyList;}
             catch(dmlException e){
                 system.debug(e);
                 GeneralConstants.mailAnyExceptionToMe('AASOpportunityCheck', 'update OptyList fail', e);
             }
         }

       /* for(Quote Quotes :Q){
            Quotes.recordtypeid = AASQuoteRecordTypeID ;
            QuoteList.add(Quotes);
        }
        if(QuoteList.size() >0){
             try{Update QuoteList;}
             catch(dmlException e){
                 system.debug(e);}
         }*/
        }    
    
    
  /*  public static void LockQuote(Set<id> OptyIds){
        //ID AASReadOnlyRecordTypeID = RecordTypeHelper.GetRecordTypeID('Quote', 'AAS Read Only');
        Map <String,Schema.RecordTypeInfo> recordTypes =  Quote.sObjectType.getDescribe().getRecordTypeInfosByName();
        Id AASReadOnlyRecordTypeID = recordTypes.get('AAS Read Only').getRecordTypeId();                
        list<Quote> Quotes = new List<Quote>();
        List<Quote> Q = [Select id, recordtypeId from Quote where Opportunityid in :OptyIds];
        for(Quote Qt :Q){
            Qt.recordtypeid = AASReadOnlyRecordTypeID ;         
            Quotes.add(Qt);
        }
        
        //try{Update Quotes;}
        //catch(DMLexception e){system.debug('Quote Record Type Could not be changed' + e);}

    }*/
    
    

     public static void ValidateFulfillmentCompleted(Set<Id> OptyId, Map<id,Opportunity> optyNewMap){
         Set<Opportunity> Op = new Set<Opportunity>();   
        List<Order__c> Orders = [select id,Opportunity__c,fulfillment_status__c from order__c where Opportunity__c in :OptyId and (fulfillment_status__c != 'Fulfillment Completed' and fulfillment_status__c!= 'Fulfillment Denied')];
        for(Order__c Fulfillment :Orders){
            Op.add(optyNewMap.get(Fulfillment.Opportunity__c));
        }
        for(Opportunity opp: Op){
            Opportunity O = optyNewMap.get(opp.id);
            O.addError('You cannot change the stage to Live unless all "Fulfillment Orders" of this Opportunity have been completed');
        }
     } //ValidateFulfillmentCompleted method ending 
    
    
}